<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="400px" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="文件名称" prop="fileName">
				<el-input v-model="form.fileName" placeholder="文件名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="关联项目" prop="proId">
				<sc-select v-model="form.proId" :apiObj="$API.project.proRecord.getProRecordListForSelect" clearable filterable style="width: 100%"></sc-select>
			</el-form-item>
			<el-form-item label="文档摘要" prop="remark">
				<el-input v-model="form.remark" placeholder="文档摘要" clearable type="textarea" maxlength="380"></el-input>
			</el-form-item>
			<el-form-item label="相关附件">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'project'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '添加项目文档',
				edit: '编辑项目文档',
				show: '查看'
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			form: {
				fileId:'',
				proId: '',
				sortNo: 0,
				fileName: '',
				remark: '',
				attachId: '',
			},
			rules: {
				fileName: [
					{required: true, message: '文档名称不能为空', trigger: 'blur'}
				],
				proId: [
					{required: true, message: '关联项目不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (this.form.attachId == '') {
					this.$alert("项目文档不能为空", "提示", {type: 'error'})
				} else {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == "add") {
							res = await this.$API.project.proFile.insertProFile.post(this.form);
						} else if (this.mode == "edit") {
							res = await this.$API.project.proFile.updateProFile.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 200) {
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				}
			})
		},
		setData(data) {
			this.form.fileId = data.fileId
			this.form.proId = data.proId
			this.form.fileName = data.fileName
			this.form.sortNo = data.sortNo
			this.form.attachId = data.attachId
			this.form.remark = data.remark
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
